package queue;
//数组模拟队列

public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(3);
        arrayQueue.addQueue(1);
        arrayQueue.addQueue(2);
        arrayQueue.addQueue(3);
        arrayQueue.getQueue();
        arrayQueue.getQueue();
        int i = arrayQueue.headQueue();
        System.out.println(i);
        arrayQueue.showQueue();
    }
}

//使用数组模拟队列
class ArrayQueue {
    private int maxSize;//最大容量
    private int front; //头
    private int rear;//尾
    private int[] arr; // 存放数据
    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.front = -1;
        this.rear = -1;
        this.arr = new int[maxSize];
    }

    //判断是否满
    public boolean ifFull() {
        return rear == maxSize - 1;
    }

    //判断队列是否为空
    public boolean isEmpty() {
        return rear == front;
    }

    //添加数据到队列
    public void addQueue(int n) {
        if (ifFull()) {
            System.out.println("队列满了不能加入");
            return;
        }
        rear++;
        arr[rear] = n;
    }

    //获取队列数据    出队列
    public int getQueue() {
        //判断队列是否为空
        if (isEmpty()) {
            throw new RuntimeException("队列为空 不能取数据");
        }
        front++;//后移
        return arr[front];
    }
    //显示所以数据
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空");
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }
    }

    //显示队列的头部
    public int headQueue() {
        if (isEmpty()) {
            System.out.println("队列开没有数据");
            throw new RuntimeException("队列开没有数据");
        }
        return arr[front + 1];
    }
}
